package kr.co.nowgugudan;

import java.util.Random;

import kr.co.nowgugudan.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.text.Editable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class ProMental extends Activity {
	InputMethodManager mbn; // 보드
	TextView l_Value; // 계산
	TextView r_Value;
	TextView oper_Value;
	EditText result;
	EditText result_vuha;
	
	int statue = 1;//게임 result의 백그라운드에 이미지가 있는지 확인하는 변수
	int number,number1; // 랜덤값
	int num,num2; // o x의 결과값
	int number_r;
	
	Vibrator mVib;
	//Handler mHandler;
	int value = 60;
	TextView texter;
	TextView result00;
	TextView result000;
	TextView result01;
	TextView result001;
	Random r = new Random();
	int mEllapse = 10;
	ProgressBar progressBar;

	WordDBHelper mHelper;
	EditText mText;
	SQLiteDatabase db;
	String a = null;
	String b = null;
	String c = null;
	String d = null;
	SoundPool soundPool;
	int tak;
	MediaPlayer mPlayer;

	ImageView tree_img[] = new ImageView[10];
	int[] img_num = new int[10];
			
	int numcase=0;
	
	int [] one={11,21,31,41,51,61,71,81,91};
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.promentalgame);
		
		mPlayer=MediaPlayer.create(this, R.raw.dingdong);
		mVib=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		tak=soundPool.load(this,R.raw.dingdong,1);
		soundPool.play(tak, 1, 1, 0, 1, 1);
		
		l_Value = (TextView) findViewById(R.id.prox);
		r_Value = (TextView) findViewById(R.id.proy);
		oper_Value = (TextView) findViewById(R.id.mult1);
		result = (EditText) findViewById(R.id.editText);
		result_vuha=(EditText) findViewById(R.id.editText_v55);
		
		mbn = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);// 아이디설정
		result00 = (TextView) findViewById(R.id.scv00);
		result01 = (TextView) findViewById(R.id.scv01);
		texter = (TextView) findViewById(R.id.textView3);

		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		result.setFocusable(false); // edit 사용 금지
		result.setClickable(false);
		result.setFocusableInTouchMode(false);
		result_vuha.setFocusable(false); // edit 사용 금지
		result_vuha.setClickable(false);
		result_vuha.setFocusableInTouchMode(false);
		
		/*Random_Replay LA=new Random_Replay();
		LA.getText(l_Value);*/
		
		number = r.nextInt(7); // 랜덤함수 설정
		l_Value.setText(number + 2 + "");
		switch(number){
		case 0:
			l_Value.setBackgroundResource(R.drawable.t2);
			break;
		case 1:
			l_Value.setBackgroundResource(R.drawable.t3);
			break;
		case 2:
			l_Value.setBackgroundResource(R.drawable.t4);
			break;
		case 3:
			l_Value.setBackgroundResource(R.drawable.t5);
			break;
		case 4:
			l_Value.setBackgroundResource(R.drawable.t6);
			break;
		case 5:
			l_Value.setBackgroundResource(R.drawable.t7);
			break;
		case 6:
			l_Value.setBackgroundResource(R.drawable.t8);
			break;
		case 7:
			l_Value.setBackgroundResource(R.drawable.t9);
			break;
		}
		number1 = r.nextInt(8);
		r_Value.setText(number1 + 1 + "");
		
		
		switch(number1){
		case 0:
			r_Value.setBackgroundResource(R.drawable.t1);
			break;
		case 1:
			r_Value.setBackgroundResource(R.drawable.t2);
			break;
		case 2:
			r_Value.setBackgroundResource(R.drawable.t3);
			break;
		case 3:
			r_Value.setBackgroundResource(R.drawable.t4);
			break;
		case 4:
			r_Value.setBackgroundResource(R.drawable.t5);
			break;
		case 5:
			r_Value.setBackgroundResource(R.drawable.t6);
			break;
		case 6:
			r_Value.setBackgroundResource(R.drawable.t7);
			break;
		case 7:
			r_Value.setBackgroundResource(R.drawable.t8);
			break;
		case 8:
			r_Value.setBackgroundResource(R.drawable.t9);
			break;
		}
	
		mHandler.sendEmptyMessage(0);
		
	}
	
	
	
	 Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			value--;
			texter.setText(""+value);
			if (value > 0) {
				mHandler.sendEmptyMessageDelayed(value, 1000);
				progressBar.incrementProgressBy(1);
			}else if (value ==0){
				if(!isFinishing()){
					showDialog(0);
				}
				
			}
		}
	};
	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
			super.onPrepareDialog(id, dialog);
			removeDialog(0);
	}
	
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		final LinearLayout linear=(LinearLayout) View.inflate(this , R.layout.order, null);
		if(id==0){
		dialog = new AlertDialog.Builder(ProMental.this)
		.setTitle("구구단")
		.setMessage("다시할래요? 그만할래요?")
		.setIcon(R.drawable.iconname)
		.setView(linear)
		.setCancelable(false)
		.setPositiveButton("종료하기",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						EditText product=(EditText) linear.findViewById(R.id.product);
						//EditText number=(EditText) linear.findViewById(R.id.number);
						mHelper = new WordDBHelper(ProMental.this);
						db = mHelper.getWritableDatabase();
						try {
							a = product.getText().toString();
							
						} catch (Exception e) {
						}
						b = result00.getText().toString();
						c = result01.getText().toString();
						db.execSQL("INSERT INTO dic VALUES (null, '"+ a + "','" + b + "','"+ c + "');");
						mHelper.close();
						finish();
					}
				})
		.setNegativeButton("다시하기",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						EditText product=(EditText) linear.findViewById(R.id.product);
						//EditText number=(EditText) linear.findViewById(R.id.number);
						mHelper = new WordDBHelper(ProMental.this);
						db = mHelper.getWritableDatabase();
						try {
							a = product.getText().toString();
							
						} catch (Exception e) {
						}
						b = result00.getText().toString();
						c = result01.getText().toString();
						db.execSQL("INSERT INTO dic VALUES (null, '"+ a + "','" + b + "','"+ c + "');");
						mHelper.close();
						
						value=60;
						num=0;
						result00.setText(num+"");
						num2=0;
						result01.setText(num2+"");
						
						progressBar.setProgress(0);
						mHandler.sendEmptyMessage(0);
						
						number = r.nextInt(7); // 랜덤함수 설정
						l_Value.setText(number + 2 + "");
						switch(number){
						case 0:
							l_Value.setBackgroundResource(R.drawable.t2);
							break;
						case 1:
							l_Value.setBackgroundResource(R.drawable.t3);
							break;
						case 2:
							l_Value.setBackgroundResource(R.drawable.t4);
							break;
						case 3:
							l_Value.setBackgroundResource(R.drawable.t5);
							break;
						case 4:
							l_Value.setBackgroundResource(R.drawable.t6);
							break;
						case 5:
							l_Value.setBackgroundResource(R.drawable.t7);
							break;
						case 6:
							l_Value.setBackgroundResource(R.drawable.t8);
							break;
						case 7:
							l_Value.setBackgroundResource(R.drawable.t9);
							break;
						}
						number1 = r.nextInt(8);
						r_Value.setText(number1 + 1 + "");
						
						
						switch(number1){
						case 0:
							r_Value.setBackgroundResource(R.drawable.t1);
							break;
						case 1:
							r_Value.setBackgroundResource(R.drawable.t2);
							break;
						case 2:
							r_Value.setBackgroundResource(R.drawable.t3);
							break;
						case 3:
							r_Value.setBackgroundResource(R.drawable.t4);
							break;
						case 4:
							r_Value.setBackgroundResource(R.drawable.t5);
							break;
						case 5:
							r_Value.setBackgroundResource(R.drawable.t6);
							break;
						case 6:
							r_Value.setBackgroundResource(R.drawable.t7);
							break;
						case 7:
							r_Value.setBackgroundResource(R.drawable.t8);
							break;
						case 8:
							r_Value.setBackgroundResource(R.drawable.t9);
							break;
						}
						
						/*finish();
						Intent intent = new Intent(ProMental.this, ProMental.class);
						startActivity(intent);*/
					}
				}).create();
		}
		return dialog;
	}

	public void mOnClick(View v) {
		mbn.hideSoftInputFromWindow(result.getWindowToken(), 0);
		MediaPlayer player;
		switch (v.getId()) {

		case R.id.result_btn25:
			if(statue==1){
				result.setBackgroundResource(R.drawable.goal);
				result_vuha.setBackgroundResource(R.drawable.goal);
			}
			int a = 0;
			int b = 0;
			int c = 0;
			a = Integer.parseInt(l_Value.getText().toString());
			b = Integer.parseInt(r_Value.getText().toString());
			try {
				c = Integer.parseInt(result.getText().toString());
			} catch (Exception e) {
			}

			if (a * b == c) {
				player=MediaPlayer.create(this, R.raw.dingdong);
				player.start();
				number = r.nextInt(7); // 랜덤함수 설정
				l_Value.setText(number + 2 + "");
				switch(number){
				case 0:
					l_Value.setBackgroundResource(R.drawable.t2);
					break;
				case 1:
					l_Value.setBackgroundResource(R.drawable.t3);
					break;
				case 2:
					l_Value.setBackgroundResource(R.drawable.t4);
					break;
				case 3:
					l_Value.setBackgroundResource(R.drawable.t5);
					break;
				case 4:
					l_Value.setBackgroundResource(R.drawable.t6);
					break;
				case 5:
					l_Value.setBackgroundResource(R.drawable.t7);
					break;
				case 6:
					l_Value.setBackgroundResource(R.drawable.t8);
					break;
				case 7:
					l_Value.setBackgroundResource(R.drawable.t9);
					break;
				}
				number1 = r.nextInt(8);
				r_Value.setText(number1 + 1 + "");
				
				switch(number1){
				case 0:
					r_Value.setBackgroundResource(R.drawable.t1);
					break;
				case 1:
					r_Value.setBackgroundResource(R.drawable.t2);
					break;
				case 2:
					r_Value.setBackgroundResource(R.drawable.t3);
					break;
				case 3:
					r_Value.setBackgroundResource(R.drawable.t4);
					break;
				case 4:
					r_Value.setBackgroundResource(R.drawable.t5);
					break;
				case 5:
					r_Value.setBackgroundResource(R.drawable.t6);
					break;
				case 6:
					r_Value.setBackgroundResource(R.drawable.t7);
					break;
				case 7:
					r_Value.setBackgroundResource(R.drawable.t8);
					break;
				case 8:
					r_Value.setBackgroundResource(R.drawable.t9);
					break;
				}
				result.setText("");
				num += 1;
				result00.setText(num + "");
			} else if (c == 0) {
				Toast.makeText(ProMental.this, "숫자를 입력해!", Toast.LENGTH_SHORT)
						.show();
			} else {
				mVib.vibrate(500);
				number = r.nextInt(7); // 랜덤함수 설정
				l_Value.setText(number + 2 + "");
				switch(number){
				case 0:
					l_Value.setBackgroundResource(R.drawable.t2);
					break;
				case 1:
					l_Value.setBackgroundResource(R.drawable.t3);
					break;
				case 2:
					l_Value.setBackgroundResource(R.drawable.t4);
					break;
				case 3:
					l_Value.setBackgroundResource(R.drawable.t5);
					break;
				case 4:
					l_Value.setBackgroundResource(R.drawable.t6);
					break;
				case 5:
					l_Value.setBackgroundResource(R.drawable.t7);
					break;
				case 6:
					l_Value.setBackgroundResource(R.drawable.t8);
					break;
				case 7:
					l_Value.setBackgroundResource(R.drawable.t9);
					break;
				}
				number1 = r.nextInt(8);
				r_Value.setText(number1 + 1 + "");
				
				
				switch(number1){
				case 0:
					r_Value.setBackgroundResource(R.drawable.t1);
					break;
				case 1:
					r_Value.setBackgroundResource(R.drawable.t2);
					break;
				case 2:
					r_Value.setBackgroundResource(R.drawable.t3);
					break;
				case 3:
					r_Value.setBackgroundResource(R.drawable.t4);
					break;
				case 4:
					r_Value.setBackgroundResource(R.drawable.t5);
					break;
				case 5:
					r_Value.setBackgroundResource(R.drawable.t6);
					break;
				case 6:
					r_Value.setBackgroundResource(R.drawable.t7);
					break;
				case 7:
					r_Value.setBackgroundResource(R.drawable.t8);
					break;
				case 8:
					r_Value.setBackgroundResource(R.drawable.t9);
					break;
				}
				result.setText("");
				num2 += 1;
				result01.setText(num2 + "");
				
			}
			
			break;

		case R.id.result_btn28:
			numcase=Integer.parseInt(result.getText().toString());
			Editable edit = result.getText();
			int st = edit.length();
			if (st > 1 && numcase>9) {
				edit.delete(st - 1, st);
				result.setText(edit);
				result_vuha.setBackgroundResource(R.drawable.goal);
			
			} else if (st <= 1 &&numcase<10) {
				result.setText("");
				result.setBackgroundResource(R.drawable.goal);
				}break;

			
		case R.id.button1_01:
			String text1= result.getText().toString();
			result.setText(text1 + "1" + "");
			
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t1);
			}else if(numcase>9&&numcase==11 ||numcase==21||numcase==31||numcase==41||numcase==41||numcase==51||numcase==61||numcase==71||numcase==81||numcase==91){
			result_vuha.setBackgroundResource(R.drawable.t1);
			}
			break;
		case R.id.button2_02:
			String text2 = result.getText().toString();
			result.setText(text2 + "2" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t2);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t2);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t2);
			}else if(numcase>9&&numcase==12 ||numcase==22||numcase==32||numcase==42||numcase==42||numcase==52||numcase==62||numcase==72||numcase==82||numcase==92){
			result_vuha.setBackgroundResource(R.drawable.t2);
			}
			break;
		case R.id.button3_03:
			String text3 = result.getText().toString();
			result.setText(text3 + "3" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t3);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t3);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t3);
			}else if(numcase>9&&numcase==13 ||numcase==23||numcase==33||numcase==43||numcase==43||numcase==53||numcase==63||numcase==73||numcase==83||numcase==93){
			result_vuha.setBackgroundResource(R.drawable.t3);
			}
			break;
		case R.id.button4_04:
			String text4 = result.getText().toString();
			result.setText(text4 + "4" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t4);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t4);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t4);
			}else if(numcase>9&&numcase==14 ||numcase==24||numcase==34||numcase==44||numcase==44||numcase==54||numcase==64||numcase==74||numcase==84||numcase==94){
			result_vuha.setBackgroundResource(R.drawable.t4);
			}
			break;
		case R.id.button5_05:
			String text5 = result.getText().toString();
			result.setText(text5 + "5" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t5);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t5);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t5);
			}else if(numcase>9&&numcase==15 ||numcase==25||numcase==35||numcase==45||numcase==45||numcase==55||numcase==65||numcase==75||numcase==85||numcase==95){
			result_vuha.setBackgroundResource(R.drawable.t5);
			}
			break;
		case R.id.button6_06:
			String text6 = result.getText().toString();
			result.setText(text6 + "6" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t6);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t6);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t6);
			}else if(numcase>9&&numcase==16 ||numcase==26||numcase==36||numcase==46||numcase==46||numcase==56||numcase==66||numcase==76||numcase==86||numcase==96){
			result_vuha.setBackgroundResource(R.drawable.t6);
			}
			break;
		case R.id.button7_07:
			String text7 = result.getText().toString();
			result.setText(text7 + "7" + "");
		/*	if(statue==1){
				result.setBackgroundResource(R.drawable.t7);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t7);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t7);
			}else if(numcase>9&&numcase==17 ||numcase==27||numcase==37||numcase==47||numcase==47||numcase==57||numcase==67||numcase==77||numcase==87||numcase==97){
			result_vuha.setBackgroundResource(R.drawable.t7);
			}
			break;
		case R.id.button8_08:
			String text8 = result.getText().toString();
			result.setText(text8 + "8" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t8);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t8);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t8);
			}else if(numcase>9&&numcase==18 ||numcase==28||numcase==38||numcase==48||numcase==48||numcase==58||numcase==68||numcase==78||numcase==88||numcase==98){
			result_vuha.setBackgroundResource(R.drawable.t8);
			}
			break;
		case R.id.button9_09:
			String text9 = result.getText().toString();
			result.setText(text9 + "9" + "");
		/*	if(statue==1){
				result.setBackgroundResource(R.drawable.t9);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t9);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				result.setBackgroundResource(R.drawable.t9);
			}else if(numcase>9&&numcase==19 ||numcase==29||numcase==39||numcase==49||numcase==49||numcase==59||numcase==69||numcase==79||numcase==89||numcase==99){
			result_vuha.setBackgroundResource(R.drawable.t9);
			}
			break;
		case R.id.button10_10:
			String text10 = result.getText().toString();
			result.setText(text10 + "0" + "");
			/*if(statue==1){
				result.setBackgroundResource(R.drawable.t0);
				statue=2;
			}else{
				result_vuha.setBackgroundResource(R.drawable.t0);
				statue=1;
			}*/
			numcase=Integer.parseInt(result.getText().toString());
			if(numcase<10){
				//result.setBackgroundResource(R.drawable.t9);
			}else if(numcase>9&&numcase==10 ||numcase==20||numcase==30||numcase==40||numcase==50||numcase==60||numcase==70||numcase==80||numcase==90){
			result_vuha.setBackgroundResource(R.drawable.t0);
			}
			break;
			
	   }
	}
 }
